package com.archway.nearshop.monitor.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DefaultMethodMonitorAspect {

	private static Logger log = LoggerFactory.getLogger("#");
	private static int TIME_WARNING=3000;
	
	@Pointcut("execution(public * com.archway..service.impl*SVImpl.*(..))")
	public void serviceLayerLog() {
	}
	
	@Around(value = "serviceLayerLog()")
	public void aroundSvTimeCost(ProceedingJoinPoint joinpoint) throws Throwable {
		log.debug("当前" + joinpoint.getSignature().getDeclaringTypeName() + "#" + joinpoint.getSignature().getName() + "方法执行开始");
		long startTime = System.currentTimeMillis();
		joinpoint.proceed();
		long endTime = System.currentTimeMillis();
		MDC.put("timecost", "耗时[" + (endTime - startTime) + "]ms.");
		log.info("当前" + joinpoint.getSignature().getDeclaringTypeName() + "#" + joinpoint.getSignature().getName() + "方法执行结束");
		MDC.remove("timecost");
		if (endTime - startTime > TIME_WARNING) {
			log.warn("当前方法耗时较长，是否应考虑优化？");
		}

		//#以后超过5秒的方法应该记录到数据库，查找原因
	}
	
}
